/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMAddManyWords;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import GuiTools.*;
import Settings.*;

/**
 * @author Christoph Schnack
 *
 */
public class SectionChooser extends JPanel{
	TribuitTextField stNewLectTF;
	JRadioButton stNewSectionRB,stToSectionRB;
	JComboBox stSectionsCB;
	String[] sections;
	Settings SET;
	Language LANG;
	
	public SectionChooser(Settings set,String[] vocSections){
		super();
		SET = set;
		LANG = set.getLanguage();
		sections = vocSections;
		stSectionsCB = (vocSections!=null)? new JComboBox(vocSections): new JComboBox(new String[0]);
		stSectionsCB.setSelectedIndex(stSectionsCB.getItemCount()-1);
		stSectionsCB.setPreferredSize(new Dimension(60,26));
		stNewLectTF = new TribuitTextField(SET,2);
		stNewSectionRB = new JRadioButton(LANG.getNew());
		stNewSectionRB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				stSectionsCB.setEnabled(false);
				stNewLectTF.setEnabled(true);
			}
		});
		stToSectionRB=new JRadioButton(LANG.getSelect());
		stToSectionRB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				stSectionsCB.setEnabled(true);
				stNewLectTF.setEnabled(false);
			}
		});
		ButtonGroup bg = new ButtonGroup();
		bg.add(stNewSectionRB);
		bg.add(stToSectionRB);
		stNewSectionRB.setOpaque(false);
		stToSectionRB.setOpaque(false);
		this.setLayout(new GridLayout(2,2));
		this.add(stToSectionRB);
		this.add(stNewSectionRB);
		this.add(stSectionsCB);
		this.add(stNewLectTF);
		if(sections!=null)stToSectionRB.doClick();
		else{
			stToSectionRB.setEnabled(false);
			stNewSectionRB.doClick();
		}
		this.setOpaque(false);
	}
	
	public String getSectionSelection(){
		if(stNewSectionRB.isEnabled()&&stNewSectionRB.isSelected())return stNewLectTF.getText().trim().equals("")?LANG.getUnnamed():stNewLectTF.getText().trim();
		if(stToSectionRB.isEnabled()&&stToSectionRB.isSelected()) return (String)stSectionsCB.getSelectedItem();
		return LANG.getUnnamed();
	}
	
	public void resetInputs(){
		stNewLectTF.setText("");
		if(stSectionsCB.getItemCount()>0)stSectionsCB.setSelectedIndex(stSectionsCB.getItemCount()-1);
	}
	
	public void setEnabledTotally(boolean enableArg){
		stToSectionRB.setEnabled(enableArg);
		stNewSectionRB.setEnabled(enableArg);
		stSectionsCB.setEnabled(enableArg);
		stNewLectTF.setEnabled(enableArg);
		if(enableArg){
			stNewLectTF.setEnabled(stNewSectionRB.isSelected());
			stSectionsCB.setEnabled(stToSectionRB.isSelected());
			if(sections == null){
				stToSectionRB.setEnabled(false);
				stSectionsCB.setEnabled(false);
				stNewSectionRB.setSelected(true);
			}
		}
	}
}
